#include "org_phnq_babel_util_IStreamWrapper.h"
#include <iostream>

using namespace std;

JNIEXPORT jint JNICALL Java_org_phnq_babel_util_IStreamWrapper_readNative__I
  (JNIEnv *env, jobject _this, jint _nativeRef)
{
	istream *in = (istream*)_nativeRef;
	return (jint)in->eof() ? -1 : in->get();
}

JNIEXPORT jint JNICALL Java_org_phnq_babel_util_IStreamWrapper_readNative__I_3BII
  (JNIEnv *env, jobject _this, jint _nativeRef, jbyteArray _bytes, jint _off, jint _len)
{
	istream *is = (istream*)_nativeRef;
	
	if(is->eof())
		return -1;

	char in[_len];
	is->read(in, _len);
	
	jint n = is->gcount();
	
	env->SetByteArrayRegion(_bytes, _off, n, (jbyte*)in);
	
	return n;
}
